package com.hiultra.archive.service.impl;

import java.util.HashMap;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hiultra.archive.domain.ArchiveBoxInoutdetail;
import com.hiultra.archive.domain.vo.ArchiveBoxInoutRecordsReportVo;
import com.hiultra.archive.domain.vo.ResultEntity;
import com.hiultra.archive.mapper.ArchiveBoxInoutdetailMapper;
import com.hiultra.archive.utils.tool.ToolUtils;
import com.hiultra.common.constant.BusinessConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.hiultra.archive.mapper.ArchiveBoxInoutRecordsMapper;
import com.hiultra.archive.domain.ArchiveBoxInoutRecords;
import com.hiultra.archive.service.IArchiveBoxInoutRecordsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 档案盒盒记录上报Service业务层处理
 *
 * @author 派大星
 * @date 2024-07-23
 */
@Service
public class ArchiveBoxInoutRecordsServiceImpl implements IArchiveBoxInoutRecordsService {
    @Autowired
    private ArchiveBoxInoutRecordsMapper archiveBoxInoutRecordsMapper;
    @Autowired
    private ArchiveBoxInoutdetailMapper archiveBoxInoutdetailMapper;

    @Value("${pubKey}")
    private String pubKey;

    @Value("${priKey}")
    private String priKey;
    @Value("${urlIp}")
    private String urlIp;

    /**
     * 查询档案盒盒记录上报
     *
     * @param id 档案盒盒记录上报主键
     * @return 档案盒盒记录上报
     */
    @Override
    public ArchiveBoxInoutRecords selectArchiveBoxInoutRecordsById(Long id) {
        return archiveBoxInoutRecordsMapper.selectArchiveBoxInoutRecordsById(id);
    }

    /**
     * 查询档案盒盒记录上报列表
     *
     * @param archiveBoxInoutRecords 档案盒盒记录上报
     * @return 档案盒盒记录上报
     */
    @Override
    public List<ArchiveBoxInoutRecords> selectArchiveBoxInoutRecordsList(ArchiveBoxInoutRecords archiveBoxInoutRecords) {
        return archiveBoxInoutRecordsMapper.selectArchiveBoxInoutRecordsList(archiveBoxInoutRecords);
    }

    /**
     * 新增档案盒盒记录上报
     *
     * @param archiveBoxInoutRecords 档案盒盒记录上报
     * @return 结果
     */
    @Override
    public int insertArchiveBoxInoutRecords(ArchiveBoxInoutRecords archiveBoxInoutRecords) {
        return archiveBoxInoutRecordsMapper.insertArchiveBoxInoutRecords(archiveBoxInoutRecords);
    }

    /**
     * 修改档案盒盒记录上报
     *
     * @param archiveBoxInoutRecords 档案盒盒记录上报
     * @return 结果
     */
    @Override
    public int updateArchiveBoxInoutRecords(ArchiveBoxInoutRecords archiveBoxInoutRecords) {
        return archiveBoxInoutRecordsMapper.updateArchiveBoxInoutRecords(archiveBoxInoutRecords);
    }

    /**
     * 批量删除档案盒盒记录上报
     *
     * @param ids 需要删除的档案盒盒记录上报主键
     * @return 结果
     */
    @Override
    public int deleteArchiveBoxInoutRecordsByIds(Long[] ids) {
        return archiveBoxInoutRecordsMapper.deleteArchiveBoxInoutRecordsByIds(ids);
    }

    /**
     * 删除档案盒盒记录上报信息
     *
     * @param id 档案盒盒记录上报主键
     * @return 结果
     */
    @Override
    public int deleteArchiveBoxInoutRecordsById(Long id) {
        return archiveBoxInoutRecordsMapper.deleteArchiveBoxInoutRecordsById(id);
    }

    /**
     * 档案室档案盒进出记录上报接口
     *
     * @param archiveBoxInoutRecords 档案盒盒记录上报
     * @return 结果
     */
    @Transactional
    @Override
    public int reportArchiveBoxInoutRecords(ArchiveBoxInoutRecords archiveBoxInoutRecords) {
        ArchiveBoxInoutRecordsReportVo archiveBoxInoutRecordsReportVo = ToolUtils.convertEntity(archiveBoxInoutRecords, ArchiveBoxInoutRecordsReportVo::new);
        String jsonString = "";
        // 将JsonData对象转换为JSON字符串
        try {
            jsonString = new ObjectMapper().writeValueAsString(archiveBoxInoutRecordsReportVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(jsonString);

        //发送请求获取数据
        HashMap hashMap = ToolUtils.sendPostRequest(BusinessConstants.ARCHIVE_BOX_STATUS_REPORT_NO, urlIp, "archiveBoxInoutRecordsReportUrl", "档案室档案盒进出记录上报", jsonString, pubKey, priKey);

        System.out.println("档案室档案盒进出记录上报接口Url" + "");
        System.out.println("档案室档案盒进出记录上报接口" + hashMap);
        if (hashMap.get("code").equals("00000")) {
            archiveBoxInoutRecords.setPushStatus(BusinessConstants.END_PUSHSTATUS);
            System.out.println("档案室档案盒进出记录上报成功");
        } else {
            System.out.println("档案室档案盒进出记录上报失败");
        }
        return archiveBoxInoutRecordsMapper.updateArchiveBoxInoutRecords(archiveBoxInoutRecords);
    }

    /**
     * 档案室档案盒进出记录定时上报接口
     *
     * @return 结果
     */
    @Override
    public ResultEntity timingArchiveBoxInoutRecordsResultReport() {
        List<ArchiveBoxInoutRecords> archiveBoxInoutRecordsList = archiveBoxInoutRecordsMapper.selectArchiveBoxInoutRecordsList(new ArchiveBoxInoutRecords() {{
            setPushStatus(BusinessConstants.PUSHSTATUS);
        }});
        if (archiveBoxInoutRecordsList.size() > 0) {
            for (ArchiveBoxInoutRecords archiveBoxInoutRecords : archiveBoxInoutRecordsList) {
                //查询子表
                List<ArchiveBoxInoutdetail> archiveBoxInoutdetailList = archiveBoxInoutdetailMapper.selectArchiveBoxInoutdetailList(new ArchiveBoxInoutdetail() {{
                    setRecordId(archiveBoxInoutRecords.getId());
                }});
                archiveBoxInoutRecords.setBoxList(archiveBoxInoutdetailList);
                reportArchiveBoxInoutRecords(archiveBoxInoutRecords);
            }
        }
        return ResultEntity.success();
    }
}
